add_library('sound')
w,h = 701,394
part_w = w/6  
part_h = h/3  
imgs = {}
old_imgs = {}
holding = False
game_state = {'holding':None,'target':None}
def setup():
    size(w,h)
    bgm = SoundFile(this, 'bgm.mp3')
    bgm.play()
    global holding
    holding = False
     
    for row in range(3):
        for col in range(6):
            img =loadImage('bg2_{}_{}.png'.format(row,col))
            old_imgs[(col,row)] = img# correct
    for row in range(3):
        for col in range(6):
            imgs[(col,row)] = old_imgs[(5 - col,row)]
            
def is_win():
    for p in imgs:
        if not old_imgs[p] == imgs[p]:
            return False
    return True
def draw():
    background(120)
    # line x ,y x, y
    fill(255)
    hold_p = game_state['holding']
    for col in range(6):
        for row in range(3):
            if hold_p == (col,row):
               
                continue
            image(imgs[(col,row)], col*part_w,row* part_h,part_w,part_h)
    if hold_p:
        image(imgs[hold_p],mouseX,mouseY,part_w,part_h)
    stroke(255)# 0-255 
    line(0 ,h / 3 * 1,w,h / 3 * 1)
    stroke(255)# 0-255 
    line(0 ,h / 3 * 2,w,h / 3 * 2)
  
    
    for i in range(6):
        stroke(255)# 0-255 
        x = w / 6 * i
        line(x,0,x,h)
    if game_state['holding'] and  game_state['target']:
       
        p1 = game_state['holding']
        p2 = game_state['target']
        imgs[p1],imgs[p2] = imgs[p2] ,imgs[p1]

        game_state['holding'] = None
        game_state['target'] = None
    if is_win():
        textSize(60)
        text("You WIn" ,w/4,h/2) 

def get_img_xy(x,y):
    return int(x/part_w),int(y/part_h)
def mousePressed():
    global holding
    print(get_img_xy(mouseX,mouseY))
    holding  = True
    if not game_state['holding'] :
        game_state['holding'] = get_img_xy(mouseX,mouseY)
    else:
        game_state['target'] = get_img_xy(mouseX,mouseY)
    print(game_state)

    